/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.server.handler;

import au.com.uptick.serendipity.server.domain.Account;
import au.com.uptick.serendipity.server.domain.dao.AccountDao;
import au.com.uptick.serendipity.shared.action.RetrieveAccountAction;
import au.com.uptick.serendipity.shared.action.RetrieveAccountResult;
import au.com.uptick.serendipity.shared.dto.AccountDto;
import com.allen_sauer.gwt.log.client.Log;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;
import org.apache.log4j.xml.DOMConfigurator;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class RetrieveAccountHandler implements ActionHandler<RetrieveAccountAction, RetrieveAccountResult> {

    // static Logger logger = Logger.getLogger(RetrieveAccountHandler.class);

    // private final Log logger;
    private final Provider<HttpServletRequest> requestProvider;
    private final ServletContext servletContext;

    @Inject
    RetrieveAccountHandler(final ServletContext servletContext,
                           final Provider<HttpServletRequest> requestProvider) {
        // this.logger = logger;
        this.servletContext = servletContext;
        this.requestProvider = requestProvider;
    }

    @Override
    public RetrieveAccountResult execute(final RetrieveAccountAction action,
                                         final ExecutionContext context) throws ActionException {

        RetrieveAccountResult result = null;
        AccountDao accountDao = new AccountDao();

        DOMConfigurator.configure("log4j.xml");

        // logger.info("Retrieve Account: " + action.getAccountId());
        Log.info("Retrieve Account: " + action.getAccountId());

        try {
            Account account = accountDao.retrieveAccount(action.getAccountId());
            // Account account = accountDao.retrieveAccount(1L);

            if (account != null) {
                result = new RetrieveAccountResult(createAccountDto(account));
            }
        } catch (Exception e) {
            // logger.warn("Unable to retrieve Account - ", e);
            Log.warn("Unable to retrieve Account - ", e);

            throw new ActionException(e);
        }
        /*
        catch (Throwable t) {
          t.printStackTrace();
        }
        */

        return result;
    }

    private AccountDto createAccountDto(Account account) {
        AccountDto accountDto = new AccountDto(account.getAccountId());
        accountDto.setAccountName(account.getAccountName());
        accountDto.setAccountNumber(account.getAccountNumber());

        return accountDto;

        // account.getAccountId(), account.getAccountName(), account.getMainPhone(),
        //    account.getCity(), account.getPrimaryContact(), account.getEmailPrimaryContact());
    }

    @Override
    public Class<RetrieveAccountAction> getActionType() {
        return RetrieveAccountAction.class;
    }

    @Override
    public void undo(RetrieveAccountAction action, RetrieveAccountResult result,
                     ExecutionContext context) throws ActionException {
    }
}

